package gods.bubble.parser;

import java.io.IOException;

import gods.bubble.common.StandardDocument;

import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.visitors.HtmlPage;

public class HtmlParser {
	public static StandardDocument parseHtml(String html) throws IOException {
		Parser parser = Parser.createParser(html, "UTF-8");
		//Parser parser = Parser.createParser(new String(html.getBytes(), "GBK"), "GBK");
		
		HtmlPage visitor = new HtmlPage(parser);

		StringBuilder sb = new StringBuilder();
		StringBuilder sc = new StringBuilder();

		try {
			parser.visitAllNodesWith(visitor);

			NodeList nodes = visitor.getBody();

			if (nodes == null) {
				return null;
			}
			
			for (Node node : nodes.toNodeArray()) {
				String s = node.toPlainTextString().trim();
				
				sc.append(node.toHtml());
				
				if (s.length() > 0)
					sb.append(node.toPlainTextString());
			}

			String content = sb.toString();
			String title = visitor.getTitle();
			
			StandardDocument sd = new StandardDocument();
			sd.title = title;
			sd.content = content;
			sd.sublinks = sc.toString();
			
			return sd;
		} catch (ParserException e) {
			System.out.println("Error in parsing html...Quit!");
		}

		return null;
	}
}
